package com.craney.testBmp
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	public class LoadPictIntoBitmap
	{
		private var parentSprite:Sprite;
		
		public function LoadPictIntoBitmap(ps:Sprite)
		{
			parentSprite = ps;
			var u:URLRequest = new URLRequest("flower.jpg");
			var l:Loader = new Loader();
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, com);
			l.load(u);
		}
		
		private function com(event:Event):void
		{
			var bmp:Bitmap = Bitmap(LoaderInfo(event.target).content);
//			parentSprite.addChild(bmp);


//以下为从一张BitmapData中读取一个矩形区域
			
			var rect:Rectangle = new Rectangle(50, 50, 100, 100);
			var bmpData:BitmapData = new BitmapData(100, 100);
			var bmpd2:BitmapData = new BitmapData(100, 100);
//使用一般读取：getPixels()			
//			var bArray:ByteArray = bmp.bitmapData.getPixels(rect);
//			bArray.position = 0;
//			for(var i:uint=0; i<50; i++)
//			{
//				for(var j:uint=0; j<50; j++)
//				{
//					bmpData.setPixel(i, j, bArray.readUnsignedInt());
//				}
//			}
//			trace(bArray.length);

//使用简单读取：copyPixels()
			
			bmpData.copyPixels(bmp.bitmapData, rect, new Point(0, 0));
			bmpd2.copyPixels(bmp.bitmapData, new Rectangle(0, 0, 100, 100), new Point(0, 0));
			
			var newBmp:Bitmap = new Bitmap(BitmapData(bmpd2.compare(bmpData)));
			parentSprite.addChild(newBmp);
			
		}

	}
}